package com.sz.feign;

import com.sz.model.Result;
import feign.hystrix.FallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

@FeignClient(value = "nbbc-transfer", fallback =ToolFeignImpl.class )
public interface ToolFeign {

    //地址
    @RequestMapping("/ip")
    public String getIp();

    //天气
    @RequestMapping(value = "/tq",method = RequestMethod.POST)
    public String getWether(@RequestBody String city);

    //获取银行卡信息
    @RequestMapping(value = "/account",method = RequestMethod.POST)
    public Result getBankCard(@RequestBody String card);

    //发送验证码
    @RequestMapping(value = "/phone")
    public Result getPhone(String phone);

    //短信
    @RequestMapping(value = "/smscode")
    public Result getPhoneSMS(@RequestParam("phone")String phone,@RequestParam ("code")String code);

    //判断付款账户余额
    @RequestMapping(value = "/amount",method = RequestMethod.POST)
    public Result getBankCardAmount(@RequestParam("amount") String amount, @RequestParam("account") String account);

}

@Component
class ToolFallbackFactory implements FallbackFactory<ToolFeign> {

    @Override
    public ToolFeign create(Throwable throwable) {
        //打印异常
        System.err.println(throwable.getMessage());

        //容错处理
        return new ToolFeign() {
            @Override
            public String getIp() {
                return "调用失败";
            }

            @Override
            public String getWether(String city) {
                return "调用失败";
            }

            @Override
            public Result getBankCard(String card) {
                return new Result(301,"调用失败");
            }

            @Override
            public Result getPhone(String phone) {
                return new Result(301,"调用失败");
            }

            @Override
            public Result getPhoneSMS(String phone, String code) {
                return new Result(301,"调用失败");
            }

            @Override
            public Result getBankCardAmount(String amount, String account) {
                return new Result(301,"调用失败");
            }
        };

    }
}